package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.PyqComment;
import com.example.service.PyqCommentService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "朋友圈评论管理") //swagger的注释
@CrossOrigin
@RequestMapping("/pyqcomment")
@RestController
public class PyqCommentController {

    @Autowired
    private PyqCommentService pyqCommentService;

    @ApiOperation(value = "获取评论")
    @GetMapping("getComment/{pyqId}")
    public R getComment(@PathVariable String pyqId) {

        QueryWrapper<PyqComment> wrapper = new QueryWrapper<>();
        wrapper.eq("pyq_id", pyqId);
        wrapper.orderByDesc("gmt_create");
        List<PyqComment> list = pyqCommentService.list(wrapper);

        return R.ok().data("list", list);
    }


    @ApiOperation(value = "添加评论")
    @PostMapping("addComment")
    public R getComment(
            @RequestBody PyqComment pyqComment
    ) {
        boolean b = pyqCommentService.save(pyqComment);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("deleteComment/{commentId}")
    public R deleteComment(@PathVariable String commentId
    ) {
        pyqCommentService.removeById(commentId);
        return R.ok();
    }


}
